package com.gitee.easypoi;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.gitee.easypoi.entity.excel.OrderAddressExcelEntity;
import com.gitee.easypoi.entity.excel.OrderExcelEntity;
import com.gitee.easypoi.entity.excel.OrderItemExcelEntity;
import com.google.common.collect.Lists;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;

/**
 * easypoi-spring-boot-starter 3.2.0版本暂不支持Spring Boot 2.X版本
 * FIXME 启动失败，等待3.3.0版本发布
 */
@RestController
@SpringBootApplication
public class EasyPoiApplication {

    public static void main(String[] args) {
        SpringApplication.run(EasyPoiApplication.class, args);
    }

    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        OrderAddressExcelEntity address = new OrderAddressExcelEntity();
        address.setAddress("上海市宝山区顾村镇菊泉新村8号201室");
        address.setContactName("林先生");
        address.setPhoneNumber("17621961592");
        OrderItemExcelEntity item1 = new OrderItemExcelEntity();
        item1.setItemName("乐事薯片");
        item1.setQuantity(BigDecimal.ONE);
        OrderItemExcelEntity item2 = new OrderItemExcelEntity();
        item1.setItemName("辣条");
        item1.setQuantity(BigDecimal.ZERO);
        OrderExcelEntity order = new OrderExcelEntity();
        order.setOrderId("TEST-001");
        order.setStatus(1);
        order.setOrderTime(new Date());
        order.setOrderAddress(address);
        order.setOrderItemList(Lists.newArrayList(item1, item2));
        ExportParams exportParams = new ExportParams("订单导出Excel", "测试");
        Workbook workbook = ExcelExportUtil.exportBigExcel(exportParams, OrderAddressExcelEntity.class, Lists.newArrayList(address));
        workbook.write(response.getOutputStream());
        response.getOutputStream().close();
    }

}
